<template>
  <div class="audit">
    <c-section title="任务处理结果审核">
      <template #right>
        <c-button type="primary" @click="submitHandle">提交</c-button>
      </template>
      <c-list ref="form" :model="formData" :rules="formRules" label-width="120px">
        <c-list-item label="审核结果" prop="reviewResult">
          <el-select v-model="formData.reviewResult" placeholder="审核结果" clearable>
            <el-option :value="1" label="同意" />
            <el-option :value="0" label="不同意" />
          </el-select>
        </c-list-item>
        <c-list-item label="审批意见" prop="reviewAdvice">
          <el-input v-model="formData.reviewAdvice" type="textarea" :autosize="{minRows: 3}" placeholder="审批意见" maxlength="1024" show-word-limit clearable />
        </c-list-item>
      </c-list>
    </c-section>
    <c-section title="设计任务处理">
      <template #right>
        <el-button type="text" @click="toSource(detail.sourceCode)">查看任务源</el-button>
      </template>
      <c-list :column="3" label-width="140px">
        <c-list-item label="任务类型" dict-type-code="TASK_TYPE" :value="detail.taskType" />
        <c-list-item label="任务分配时间" :format-time="false" :value="detail.distributeTime" />
        <c-list-item label="门店建设项目号" :value="detail.constCode" />
        <c-list-item label="所属门店编码" :value="shopDetail.shopCode" />
        <c-list-item label="所属门店" :value="shopDetail.shopName" />
        <c-list-item label="所属客户编码" :value="shopDetail.clientCode" />
        <c-list-item label="所属客户" :value="shopDetail.clientName" />
        <c-list-item label="所属办事处" :value="shopDetail.officeName" />
        <c-list-item label="门店建设范围" dict-type-code="DECORATION_RANGE" :value="detail.decorationRange" />
        <c-list-item label="装修形式" dict-type-code="DECORATION_FORM" :value="detail.decorationType || detail.decorationForm" />
        <c-list-item label="是否需要复尺" dict-type-code="BOOLEANS" :value="detail.isNeedResurvey" />
        <c-list-item label="是否完成复尺" dict-type-code="BOOLEANS" :value="detail.isEndResurvey" />
        <c-list-item label="不复尺设计摆场" dict-type-code="UNRESURVEY_STATE" :value="detail.unresurveyDesignSetup" />
        <c-list-item label="不复尺设计施工" dict-type-code="UNRESURVEY_STATE" :value="detail.unresurveyDesignConst" />
      </c-list>
      <!-- 十月优化START -->
      <Area :detail="detail" :pic-detail="picDetail" :design-detail="designDetail" />
      <!-- 十月优化END -->
      <c-list :column="3" label-width="140px">
        <c-list-item label="设计单位" :value="detail.designUnitName" />
        <c-list-item label="申请说明" :span="3" :value="detail.taskDesc" />
        <c-list-item label="卖场预计设计完成时间" :value="detail.marketExpectCompleteTime" />
        <c-list-item label="门头预计设计完成时间" :value="detail.dheadExpectCompleteTime" />
        <c-list-item label="是否KA卖场" dict-type-code="BOOLEANS" :value="detail.isKaMarket" />
        <c-list-item label="是否重点市场" dict-type-code="BOOLEANS" :value="detail.isFocusMarket" />
        <c-list-item label="图纸紧急程度" dict-type-code="PIC_URGENT_LEVEL" :value="detail.picUrgentLevel" />
        <c-list-item label="店面等级" dict-type-code="SHOP_LEVEL_CODE" :value="detail.shopLevelCode" />
        <c-list-item label="是否重复设计" dict-type-code="BOOLEANS" :value="detail.haveRedesign" />
        <c-list-item label="是否需要门头效果图" dict-type-code="BOOLEANS" :value="designDetail.isNeedRendering" />

        <c-list-item label="任务处理方式" :span="3">
          <el-checkbox-group v-model="taskHandelWay" disabled>
            <el-checkbox v-for="ht in handleTypeList" :key="ht.dictCode" :label="ht.dictCode">{{ ht.dictName }}</el-checkbox>
          </el-checkbox-group>
        </c-list-item>
      </c-list>
    </c-section>
    <c-section title="图纸变更结果（仅显示本次新增/变更的图纸）">
      <c-table-box index hide-func :table-height="400" :data="data" :table-config="tableConfig" />
    </c-section>
  </div>
</template>
<script>
import { getShopDesignTaskInfo, shopConstructPage, getShopDesignTaskWholeInfo, putTaskExamine, basicsInfo, getShopDesignInfo } from '@/api/shopDesignTask'
import { SLMDict } from '@/api/dataDict'
import { getPath, getPicturePage } from './getPath'
import { concatObj } from '@/utils'
import Area from './components/Area'
export default {
  name: 'ShopDesignTaskAudit',
  components: {
    Area
  },
  data() {
    return {
      allConfigArea: [],
      taskHandelWay: [],
      designDetail: {},
      taskDetail: [],
      picDetail: {},
      shopDetail: {},
      wholeInfo: {},
      editTableData: [],
      tableData: [],
      tableConfig: {
        setting: [
          {
            prop: 'picType',
            label: '图纸类型',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'PIC_TYPE'
            },
            minWidth: 100
          },
          // {
          //   prop: 'picTaskCode',
          //   label: '图纸任务编码'
          // },
          {
            prop: 'picTaskName',
            label: '图纸名称',
            minWidth: 120
          },
          {
            prop: 'designRange',
            label: '设计范围',
            render: (h, row) => h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.designRange
              }
            }),
            minWidth: 120
          },
          {
            prop: 'designSpace',
            label: '设计区域',
            render: (h, row) => {
              return h('c-design-area', { props: { value: row.designSpace }})
            },
            minWidth: 120
          },
          {
            prop: 'designBizType',
            label: '设计业务类型',
            minWidth: '180',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'BIZ_TYPE'
            }
          },
          {
            prop: 'decorationType',
            label: '装修形式',
            minWidth: '100',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'DECORATION_FORM'
            }
          },
          {
            prop: 'picTotalArea',
            label: '图纸总面积',
            minWidth: 120
          },
          {
            prop: 'softwareSumArea',
            label: '软体区总面积',
            minWidth: '120'
          },
          {
            prop: 'suiteSumArea',
            label: '套房区总面积',
            minWidth: '120'
          },
          {
            prop: 'newBusinessAreaSummary',
            label: '新业务总面积',
            minWidth: 120
          },
          {
            prop: 'haveEAndC',
            label: '是否含成融定',
            minWidth: '100',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'BOOLEANS'
            }
          },
          {
            prop: 'eandCArea',
            label: '成融定面积',
            minWidth: 120
          },
          {
            prop: 'isRedesign',
            label: '是否重复设计',
            minWidth: '120',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'BOOLEANS'
            }
          },
          {
            prop: 'sourcePicName',
            label: '来源图纸',
            minWidth: 120
          },
          {
            prop: 'designerName',
            label: '设计师',
            minWidth: 120
          },
          {
            prop: 'state',
            label: '当前图纸状态',
            width: 120,
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'PIC_DESIGN_STATE'
            }
          },
          {
            prop: 'taskHandleType',
            label: '任务处理结果',
            render: (h, row) => {
              // 处理任务方式 0修改,1暂停，2恢复,3作废
              return h('span', this.taskHandleTypeName(row.taskHandleType))
            },
            minWidth: 100
          }
        ]
      },
      formData: {
        reviewResult: '',
        reviewAdvice: ''
      },
      formRules: {
        reviewResult: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
        reviewAdvice: [{ required: true, message: '请输入审批意见', trigger: 'change' }]
      },
      handleTypeList: []
    }
  },
  computed: {
    data() {
      return [...this.tableData, ...this.editTableData]
    },
    detail() {
      return concatObj(this.picDetail, this.taskDetail, this.wholeInfo, this.designDetail, this.shopDetail)
      // return { ...this.designDetail, ...this.taskDetail }
    }
  },
  watch: {
    detail(d) {
      const { reviewAdvice, reviewResult } = d
      if (reviewAdvice !== undefined && reviewAdvice !== null) {
        this.$set(this.formData, 'reviewAdvice', reviewAdvice)
      }
      if (reviewResult !== undefined && reviewResult !== null) {
        this.$set(this.formData, 'reviewResult', parseInt(reviewResult))
      }
    }
  },
  created() {
    this.getDetail()
    this.getHandleType()
  },
  methods: {
    async getDetail() {
      await getShopDesignTaskInfo(this.$route.query.id).then(res => {
        this.taskDetail = res.data
        shopConstructPage(res.data.constCode).then(res => {
          this.shopDetail = res.data.records.length > 0 ? res.data.records[0] : {}
        })
        this.editTableData = res.data.shopDesignPictureChangeList || []
        this.tableData = res.data.shopDesignPictureDTOList || []
        this.taskHandelWay = typeof this.taskDetail.taskHandelWay === 'string' ? this.taskDetail.taskHandelWay.split(',') : []
        if (this.taskDetail.taskType === 'DRAWING_TRIGGER') {
          getPicturePage(this.taskDetail.sourceCode).then(data => {
            this.picDetail = data
          })
        }
        // 获取设计信息
        getShopDesignInfo(res.data.constCode).then(res => {
          this.designDetail = res.data
        })
      })
      if (this.taskDetail.taskType === 'DESIGN_CHANGE') {
        await basicsInfo(this.$route.query.id).then(res => {
          this.wholeInfo = res.data
        })
      } else {
        // 获取全部详情  设计变更报错
        await getShopDesignTaskWholeInfo(this.$route.query.id).then(res => {
          this.wholeInfo = res.data
        })
      }
    },
    getHandleType() {
      SLMDict('TASK_HANDEL_WAY').then(res => {
        this.handleTypeList = res.data.filter(e => {
          return !['DISTRIBUTION', 'TERMINATE'].includes(e.dictCode)
        })
      })
    },
    taskHandleTypeName(v) {
      // 处理任务方式 0修改,1暂停，2恢复,3作废
      const t = { 0: '修改', 1: '暂停', 2: '恢复', 3: '作废', 4: '新增' }
      v = (v === undefined || v === null) ? '' : v.toString()
      const names = []
      this.$split(v).forEach(e => {
        if (t[e]) {
          names.push(t[e])
        }
      })
      return names.join(',')
    },
    submitHandle() {
      return this.$refs.form.validate().then(() => {
        const data = { ...this.formData }
        data.id = this.$route.query.id
        data.constCode = this.detail.constCode
        return putTaskExamine(data).then(res => {
          this.$message.success('提交成功！')
          this.$store.dispatch('views/goFirst')
        })
      })
    },
    async toSource(v) {
      const { path, query } = await getPath(this.detail.taskType, v)
      if (!path) return
      query ? query.time = new Date().getTime() : ''
      this.$store.dispatch('views/goPage', { path, query })
    }
  }
}
</script>
